<?php
/**
 * 标签
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Comment;
use App\Models\Live;
use App\Models\Movie;
use App\Models\MovieCategory;
use App\Models\User;
use App\Models\UserGiftLog;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class RzroadController extends ApiController
{
    public function index()
    {
        $type = request()->input('type');
        $data=[];
        if($type==12){
            $data=$this->readcount();
        }elseif ($type==11){
            $data=$this->topiccount();
        }
        return $this->success('成功', $data);
    }
    //最热阅读信息30
    //created_at
    //view_num
    public function readcount()
    {
        $oldtime=Carbon::today()->subDays(30)->timestamp;//前30天的时间戳
        $data=Article::with(['user'=>function($query){
            $query->select('id','username');
            }])
            ->whereBetween('created_at',[$oldtime,time()])
            ->where('type',1)
            ->groupBy('view_num')
            ->orderBy('view_num', 'desc')
            ->limit(5)
            ->get()
            ->toArray();
        return $data;
    }
    //最新评论
    public function topiccount()
    {
        $oldtime=Carbon::today()->subDays(30)->timestamp;//前30天的时间戳
        $data=Comment::with(['user'=>function($query){
            $query->select('id','username');
        },'article'=>function($query){
            $query->select('id','title');
        },'video'=>function($query){
            $query->select('id','title');
        }])
            ->whereBetween('created_at',[$oldtime,time()])
            ->where('type',1)
            ->orderBy('created_at', 'desc')
            ->limit(5)
            ->get()
            ->toArray();
        foreach ($data as $k=>$v){
            if($v['type']==1){
                $data[$k]['title']=$v['video']['title'];
            }
            if($v['type']==2){
                $data[$k]['title']=$v['article']['title'];
            }
        }
        return $data;
    }

}
